//202102104015柴亚楠
package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.ActorAssRoleDao;
import cn.edu.sdjzu.xg.kcsj.domain.authority.Actor;
import cn.edu.sdjzu.xg.kcsj.domain.authority.ActorAssRole;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class ActorAssRoleService {
    private static final ActorAssRoleDao actorAssRoleDao = ActorAssRoleDao.getInstance();
    private static final ActorAssRoleService actorAssRoleService = new ActorAssRoleService();

    private ActorAssRoleService() {
    }

    public static ActorAssRoleService getInstance() {
        return actorAssRoleService;
    }
    public Collection<ActorAssRole> findByActorId(Actor actor) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<ActorAssRole> actorAssRoles = actorAssRoleDao.findByActorId(connection, actor);
        JdbcHelper.close(connection);
        return actorAssRoles;
    }

    public Collection<ActorAssRole> findAll() throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<ActorAssRole> actorAssRoles = actorAssRoleDao.findAll(connection);
        JdbcHelper.close(connection);
        return actorAssRoles;
    }

    public ActorAssRole find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        ActorAssRole actorAssRole = actorAssRoleDao.find(connection, id);
        JdbcHelper.close(connection);
        return actorAssRole;

    }

    public boolean update(ActorAssRole actorAssRole) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = actorAssRoleDao.update(connection, actorAssRole);
            connection.commit();
        }catch (Exception e){
            connection.rollback();
            e.printStackTrace();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;

    }

    public boolean add(ActorAssRole actorAssRole) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            added = actorAssRoleDao.add(connection, actorAssRole);
            connection.commit();
        }catch (Exception e){
            connection.rollback();
            e.printStackTrace();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            deleted = actorAssRoleDao.delete(connection, id);
            connection.commit();
        }catch (Exception e){
            connection.rollback();
            e.printStackTrace();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean deletes(List<ActorAssRole> listToDelete) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            for(ActorAssRole e:listToDelete) {
                int id= e.getId();
                deleted = actorAssRoleDao.delete(connection,id);
            }
            connection.commit();
        }catch (Exception e){
            connection.rollback();
            e.printStackTrace();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted ;
    }
}

